
package com.aris.voicechecking;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.text.style.SuperscriptSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicecheck.provider.DataProviderV2;

public class MainActivity extends Activity {

    private Button button_01;

    private Button button_02;

    private Button button_03;

    private RelativeLayout adView;

    private ImageView lvl_picture;

    private TextView ratio;

    private DataProviderV2 provider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initial();
        execution();
    }

    @Override
    protected void onResume() {

        super.onResume();
        setupLevel();
    }

    private void initial() {
        button_01 = (Button)findViewById(R.id.button_01);
        button_02 = (Button)findViewById(R.id.button_02);
        button_03 = (Button)findViewById(R.id.button_03);
        adView = (RelativeLayout)findViewById(R.id.adView);
        lvl_picture = (ImageView)findViewById(R.id.lvl_picture);
        ratio = (TextView)findViewById(R.id.ratio);
        provider = new DataProviderV2(this);
    }

    private void execution() {

        button_01.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent lessonIntent = new Intent(MainActivity.this, StudingModeActivity.class);
                startActivity(lessonIntent);
                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);
            }
        });

        button_02.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent lessonResultIntent = new Intent(MainActivity.this,
                        LessonResultActivity.class);
                startActivity(lessonResultIntent);
                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);
            }
        });

        button_03.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent lessonResultIntent = new Intent(MainActivity.this, ConnectionActivity.class);
                startActivity(lessonResultIntent);
                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);
            }
        });

        adView.setBackgroundResource(R.drawable.banner);

        adView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent webviewIntent = new Intent(MainActivity.this, WebviewActivity.class);
                startActivity(webviewIntent);
                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);

                //                Uri uriUrl = Uri.parse(ConnectionProvider.BANNER_URL);
                //                Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
                //                startActivity(launchBrowser);

            }
        });

    }

    private void setupLevel() {
        Double temppercent = provider.calculatorPoint();

        // DecimalFormat formatter = new DecimalFormat("#0.00");
        // String temp = formatter.format(percent);

        int percent = (int)Math.round(temppercent);

        String astr = percent + "%";

        SpannableString ss1 = new SpannableString(astr);
        // ss1.setSpan(new RelativeSizeSpan(0.2f), 0, ss1.length(), 0);
        ss1.setSpan(new SuperscriptSpan(), astr.length() - 1, astr.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss1.setSpan(new RelativeSizeSpan(0.5f), astr.length() - 1, astr.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // ss1.setSpan(new SuperscriptSpan(), 6, 7,
        // Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // ss1.setSpan(new RelativeSizeSpan(0.75f), 6, 7,
        // Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // ratio.setText(Html.fromHtml(a +
        // "<sup><span style=\"font-size:0.8em\">%</span></sup>"));
        // ratio.append(a + "");
        // ratio.append(ss1);
        ratio.setText(ss1);

        int idlvl;

        if (percent == 100) {
            idlvl = R.drawable.lvl0;
        } else if (percent >= 80) {
            idlvl = R.drawable.lvl1;
        } else {
            if (percent >= 60) {
                idlvl = R.drawable.lvl2;
            } else {
                if (percent >= 40) {
                    idlvl = R.drawable.lvl3;
                } else {
                    if (percent >= 20) {
                        idlvl = R.drawable.lvl4;
                    } else {
                        idlvl = R.drawable.lvl5;
                    }
                }
            }
        }

        lvl_picture.setImageResource(idlvl);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// ***Change Here***
        startActivity(intent);
        finish();
        System.exit(0);
    }

}
